import { getSnCodeOptionListBySparePartId } from '@/api/code'
import { getOrderSparePageSelect } from '@/api/order'
import { checkOrderSpare, getBeforeCheckOrderSpareDetail, getOrderSpareMaterial } from '@/api/parts'
import { ref } from 'vue'

/**
 * 获取工单配件下拉选项列表
 */
export const useGetOrderSparePartOptions = () => {
  const getSparePartOptionsLoading = ref(false)
  const getSparePartOptionsParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })
  const sparePartOptions = ref<Array<OrderSparePartAPIResponse.SparePartOption>>([])

  const getSparePartOptions = async (orderId: string) => {
    try {
      getSparePartOptionsLoading.value = true
      const res = await getOrderSparePageSelect(getSparePartOptionsParam.value, orderId)
      getSparePartOptionsLoading.value = false
      if (res.data.code === 20000) {
        sparePartOptions.value = res.data.data.rows
        return ''
      }
      return res.data.message
    } catch (error) {
      return '获取配件列表失败'
    }
  }

  return {
    sparePartOptions,
    getSparePartOptionsParam,
    getSparePartOptionsLoading,
    getSparePartOptions,
  }
}

/**
 * 根据配件id获取序列号下拉筛选
 */
export const useGetSnCodeOptionListBySparePartId = () => {
  // 获取下拉选项加载
  const getOptionListLoading = ref(false)

  // 获取下拉选项请求参数
  const getOptionListParam = ref({
    spareId: '',
    code: '',
  })

  // 下拉选项
  const optionList = ref<Array<OrderSparePartAPIResponse.SnCodeOptionBySparePartId>>([])

  /**
   * 获取序列号下拉选项
   */
  const getSnCodeOptionList = async () => {
    try {
      getOptionListLoading.value = true
      const res = await getSnCodeOptionListBySparePartId(getOptionListParam.value)
      getOptionListLoading.value = false
      if (res.data.code === 20000) {
        optionList.value = res.data.data
        return ''
      }
      return res.data.message
    } catch (error) {
      getOptionListLoading.value = false
      return '获取合格证下拉选项失败，请稍后重试'
    }
  }

  return {
    snCodeOptionList: optionList,
    getSnCodeOptionListParam: getOptionListParam,
    getSnCodeOptionListLoading: getOptionListLoading,
    getSnCodeOptionList: getSnCodeOptionList,
  }
}

/**
 * 获取工单配件审核前的信息
 */
export const useGetOrderSparePartInfoBeforeCheck = () => {
  // 获取信息加载
  const getInfoLoading = ref(false)

  // 信息
  const info = ref<OrderSparePartAPIResponse.OrderSparePartInfoBeforeCheck>({
    applyEmp: '',
    applyEmpType: 0,
    applyRemark: '',
    code: '',
    createTime: '',
    id: '',
    networkSpareType: 0,
    oldCopeWay: 1,
    receiptAddrType: 1,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    received: false,
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: null,
    status: 0,
    type: 1,
    updateTime: '',
    userOrNetworkId: '',
    workId: '',
    workNo: '',
  })

  /**
   * 获取信息
   * @param sparePartId - 工单配件id
   */
  const getInfo = async (sparePartId: string) => {
    try {
      getInfoLoading.value = true
      const res = await getBeforeCheckOrderSpareDetail(sparePartId)
      getInfoLoading.value = false
      if (res.data.code === 20000) {
        info.value = res.data.data.row
        return ''
      }
      return res.data.message
    } catch (error) {
      getInfoLoading.value = false
      return '获取工单配件信息失败'
    }
  }

  return {
    sparePartInfoBeforeCheck: info,
    getSparePartInfoBeforeCheckLoading: getInfoLoading,
    getSparePartInfoBeforeCheck: getInfo,
  }
}

/**
 * 工单配件审核表单
 */
export const useSparePartCheckForm = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref<OrderSparePartAPIRequest.SparePartCheckForm>({
    id: '',
    oldCopeWay: 1,
    oldReceiptAddress: '',
    oldReceiptArea: '',
    oldReceiptCity: '',
    oldReceiptName: '',
    oldReceiptPhone: '',
    oldReceiptProvince: '',
    oldReceiptTown: '',
    receiptAddrType: 1,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    remark: '',
    storeId: '',
    storePositionId: '',
    userOrNetworkId: '',
    workSpareMaterials: [],
  })

  /**
   * 重置审核表单
   */
  const resetCheckForm = () => {
    checkForm.value = {
      id: '',
      oldCopeWay: 1,
      oldReceiptAddress: '',
      oldReceiptArea: '',
      oldReceiptCity: '',
      oldReceiptName: '',
      oldReceiptPhone: '',
      oldReceiptProvince: '',
      oldReceiptTown: '',
      receiptAddrType: 1,
      receiptAddress: '',
      receiptArea: '',
      receiptCity: '',
      receiptName: '',
      receiptPhone: '',
      receiptProvince: '',
      receiptTown: '',
      remark: '',
      storeId: '',
      storePositionId: '',
      userOrNetworkId: '',
      workSpareMaterials: [],
    }
  }

  const submit = async () => {
    try {
      checkLoading.value = true
      const res = await checkOrderSpare(checkForm.value)
      checkLoading.value = false
      return res.data
    } catch (error) {
      checkLoading.value = false
    }
  }

  return {
    orderSparePartCheckForm: checkForm,
    orderSparePartCheckLoading: checkLoading,
    orderSparePartCheck: submit,
    resetOrderSparePartCheckForm: resetCheckForm,
  }
}

/**
 * 根据工单物料单id获取物料单下的物料列表
 */
export const useGetSparePartListByOrderSparePartId = () => {
  // 获取物料列表加载
  const getListLoading = ref(false)

  // 物料列表
  const list = ref<Array<OrderSparePartAPIResponse.SparePartListItemBeforeCheck>>([])

  /**
   * 获取物料列表
   * @param sparePartId - 工单物料单id
   */
  const getList = async (sparePartId: string) => {
    try {
      getListLoading.value = true
      const res = await getOrderSpareMaterial(sparePartId)
      getListLoading.value = false
      if (res.data.code === 20000) {
        list.value = res.data.data.rows
        return ''
      }
      return res.data.message
    } catch (error) {
      return '获取物料失败，请稍后重试'
    }
  }

  return {
    sparePartListBeforeCheck: list,
    getSparePartListBeforeCheckLoading: getListLoading,
    getSparePartListBeforeCheck: getList,
  }
}
